package cadastrarEmpresa;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import negocio.Empresa;
import util.ConnectionHelper;
import util.ServletHelper;
import dao.RepositorioEmpresa;

/**
 * Servlet implementation class ServletCadastrarEmpresa
 */
@WebServlet(name = "ServletCadastrarEmpresa", urlPatterns = { "/cadastrarEmpresa" })
public class ServletCadastrarEmpresa extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
    public ServletCadastrarEmpresa() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Empresa empresa = new Empresa();
		
		empresa.setDescricao(request.getParameter("inputDescricao"));
		empresa.setNome(request.getParameter("inputNome"));
		empresa.setEmail(request.getParameter("inputEmail"));
		empresa.setEndereco(request.getParameter("inputEndereco"));
		empresa.setSenha(request.getParameter("inputEmail"));
		
		RepositorioEmpresa rep = new RepositorioEmpresa();
		int status = rep.cadastrarEmpresa(empresa);
		
		//int status = rep.cadastrarEmpresaStatment(empresa);
		
		System.out.println(ConnectionHelper.verificarStatus(status));
		
		int id = rep.consultarIdEmpresa(empresa.getEmail());
		
		HttpSession  session = request.getSession();
		
		session.setAttribute("empresaNome", empresa.getNome());
		session.setAttribute("idEmpresa", id);
		
		ServletHelper.forward(request, response, "cadastrarEmpresa.jsp");
	}

}
